(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



(* ::Input::GrayLevel[0]:: *)
(*  TContinuumMechanics version 3.0 is a Mathematica package that needs the package Tensorial 5.0 *)


(* ::Input::GrayLevel[0]:: *)
(*  Author: Jean-Fran\[CCedilla]ois Gouyet  *)


(* ::Input::GrayLevel[0]:: *)
(*  jf.gouyet@aliceadsl.fr *)


(* ::Input::GrayLevel[0]:: *)
(* This software package and its accompanying documentation are provided without 
 guarantee of support or maintenance *)


(* ::Input::GrayLevel[0]:: *)
(*  Mathematica is a registered trademark of Wolfram Research, Inc.     *)


(* ::Input::GrayLevel[0]:: *)
BeginPackage["TContinuumMechanics3`TContinuumMechanics`",{"TensorCalculus5`Tensorial`","Presentations`Manipulations`"}]


(* ::Input::GrayLevel[0]:: *)
HoldFactor::usage="HoldFactor[term][expr] allows a formula presentation of expr in which a given term in expr is put in factor in an HoldForm to prevent unexpected simplification.";


(* ::Input::GrayLevel[0]:: *)
PartialSumHold::usage="PartialSumHold[baseindex, newindices][expr] will expand dummy summations of a tensor expression into components that have one or more baseindex and unexpanded remainders, as PartialSum does. But now it will act on HoldForms in such a way that this HoldForm be preserved.";


(* ::Input::GrayLevel[0]:: *)
ToFlavorD::usage="ToFlavorD[toflavor, fromflavor][expr] is eqivalent to ToFlavor[toflavor, fromflavor][expr], except in the case of two-dimensional derivations where it is needed.";


(* ::Input::GrayLevel[0]:: *)
HoldCovariantD::usage="HoldCovariantD[expr, i] represents the unexpanded covariant derivative of expr with respect to the index i.\n HoldCovariantD[expr,{i,j,...}] represents the covariant unexpanded derivative with respect to the list of indices. \n Symbols are considered constants.";


(* ::Input::GrayLevel[0]:: *)
GhostCovariantD2d::usage="GhostCovariantD2d[expr, i] represents the 2-dimensional unexpanded covariant derivative of expr with respect to the index i.\n GhostCovariantD2d[expr,{i,j,...}] represents the 2-dimensional covariant unexpanded derivative with respect to the list of indices. \n Symbols are considered constants.";


(* ::Input::GrayLevel[0]:: *)
HoldCovariantD2d::usage="HoldCovariantD2d[expr, i] represents the 2-dimensional unexpanded covariant derivative of expr with respect to the index i.\n HoldCovariantD2d[expr,{i,j,...}] represents the 2-dimensional covariant unexpanded derivative with respect to the list of indices. \n Symbols are considered constants.";


(* ::Input::GrayLevel[0]:: *)
HoldContravariantD::usage="HoldContravariantD[tensor, i] represents the unexpanded contravariant derivative of the tensor with respect to the index i.\n HoldContravariantD[tensor,{i,j,...}] represents the contravariant unexpanded derivative with respect to the list of indices. \n Symbols are considered constants.";


(* ::Input::GrayLevel[0]:: *)
HoldContravariantD2d::usage="HoldContravariantD2d[tensor, i] represents the 2-dimensional unexpanded contravariant derivative of the tensor with respect to the index i.\n HoldContravariantD2d[tensor,{i,j,...}] represents the 2-dimensional contravariant unexpanded derivative with repect to the list of indices. \n Symbols are considered constants.";


(* ::Input::GrayLevel[0]:: *)
BasisChange::usage=
"BasisChange[\[Beta],toflavor][Tensor[T,list1,list2]] allows to perform the change via the transformation \[Beta], of an arbitrary tensor T in an initial flavor,to the final flavor 'toflavor'.";


(* ::Input::GrayLevel[0]:: *)
BasisDerivation::usage=
"BasisDerivation[m][{\[DoubleStruckE],i},j][expr] expresses the partial derivative with respect to index j, of a basis vector Tensor[\[DoubleStruckE],{Void},{i}] present in expr, into a combination of basis vectors. The coefficients are Christoffel symbols; m is a dummy index. The parameters are defined in labels ={x, \[Delta], g, \[CapitalGamma]}. This function includes the assignement to zero of the covariant derivative of basis vectors.
BasisDerivation[h,m][{\[DoubleStruckE],i},{j,k}][expr] do the same kind of expansion for second partial derivatives. It includes the assignement to zero of the covariant derivatives of basis vectors.";


(* ::Input::GrayLevel[0]:: *)
StandardDownIndices::usage=
"StandardDownIndices[g][expr] rewrites all the tensors in expr with only down indices, with the introduction of the metric tensor g. This allows to have a standard writing of expr. It is particularly convenient when the basis is orthonormal (g is the identity matrix, and in this case many authors do not even introduce upper indices).";


(* ::Input::GrayLevel[0]:: *)
SymmetricStandardOrder::usage=
"\!\(\*
StyleBox[\"SymmetricStandardOrder\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"[\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"x\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\",\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"{\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"ind1\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\".\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"ind2\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"}\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"]\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"[\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"expr\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"]\",\nFontWeight->\"Bold\"]\) is used specifically in presence of covariant derivatives.  So as to compare in a standard way identical expressions, \!\(\*
StyleBox[\"SymmetricStandardOrder\",\nFontWeight->\"Bold\"]\) orders vertical couples {flavor@i,Void}, ... of indices, including their flavor, any symmetric tensor T present in \!\(\*
StyleBox[\"expr\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)and its derivatives. Upper (or contravariant) indices are ordered first in alphabetic order on the left, then lower (or covariant) indices.  It allows to order the symmetrical indices between columns 'ind1' and 'ind2', ordering (if allowed) separately the indices of the tensor T itself, and of the covariant indices. \!\(\*
StyleBox[\"SymmetricStandardOrder\",\nFontWeight->\"Bold\"]\) does not work in the case of mixtures of 2d and 3d derivations.
It has some common behaviour with SymmetrizeSlots to which it is complementary, but it also orders symmetric tensors containing covariant derivatives which is not possible with SymmetrizeSlots.";


(* ::Input::GrayLevel[0]:: *)
AntiSymmetricStandardOrder::usage=
"AntiSymmetricStandardOrder[x][expr] allows to order vertical couples {red@i,Void}, ... of indices, irrespective of their flavor, in an antisymmetric tensor x present in expr. Upper indices are ordered first in alphabetic order on the left, then lower indices. It has some common behaviour with SymmetrizeSlots to which it is complementary.";


(* ::Input::GrayLevel[0]:: *)
MetricSimplifyD::usage=
"MetricSimplifyD[g][expr] will raise, lower or replace indices in product terms involving g and covariant or contravariant derivatives of tensors in expr using g as the label for a metric tensor. This is possible because covariant and contravariant derivatives commute with g.";


(* ::Input::GrayLevel[0]:: *)
UpDownSwapD::usage=
"UpDownSwapD[dummyindex][term] will swap the up-down positions of the dummy index involving covariant or contravariant derivatives in the term consisting of simple tensor products.";


(* ::Input::GrayLevel[0]:: *)
RaiseIndexD::usage="RaiseIndexD[i,j][tensor] will raise the lower index i in covariant derivatives and rename it j.\nRaiseIndex[i,j][expr] will raise the index in all Tensors in the expr.";


(* ::Input::GrayLevel[0]:: *)
LowerIndexD::usage="LowerIndexD[i,j][tensor] will lower the upper index i in contravariant derivatives and rename it j.\nLowerIndex[i,j][expr] will lower the index in all Tensors in the expr.";


(* ::Input::GrayLevel[0]:: *)
KroneckerAbsorbD::usage=
"KroneckerAbsorbD[\[Delta]][expr]\*
StyleBox[\( \!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\)]\!\(\*
StyleBox[\"will\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"perform\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"inside\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"HoldForms\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"of\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"covariant\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"derivatives\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\",\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"all\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"replacement\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"operations\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"of\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"first\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"order\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"mixed\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"tensors\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"with\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"labels\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"\[Delta]\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\",\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"assumed\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"to\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"be\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"Kronecker\",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\" \",\nFontWeight->\"Bold\"]\)\!\(\*
StyleBox[\"deltas\",\nFontWeight->\"Bold\"]\).";


(* ::Input::GrayLevel[0]:: *)
PartialSumD::usage=
"PartialSumD[i,j][expr] will expand the dummy indices of a tensor expression containing derivatives, into components that have one or more baseindex indices i and an unexpanded remainder j. Symbolic indices are replaced with newindices.";


(* ::Input::GrayLevel[0]:: *)
MetricRuleD::usage=
"MetricRuleD[x,g][expr] rewrites covariant derivatives of basis coordinates x in expr, into the (mixed) metric tensor g, and simplify its value to 1 if the derivation is on the same coordinate and to 0 otherwise. The result is left non evaluated if an index of g is literal.";


(* ::Input::GrayLevel[0]:: *)
Kronecker::usage=
"Kronecker[\[Delta]][expr] gives the Kronecker delta \[Delta] in expr, equal to 1 if the indices with the same flavor are equal, and 0 otherwise.";


(* ::Input::GrayLevel[0]:: *)
ChristoffeluSymbol::usage=
"ChristoffeluSymbol[\[DoubleStruckE],\[CapitalGamma],k][expr] expressed the partial derivatives PartialD[Tensor[\[DoubleStruckE],{Void},{i}],j] of basis vectors '\[DoubleStruckE]' on the basis using '\[CapitalGamma]'-noted Christoffel symbols with up indices; k is the dummy index of Einstein summation. We have chosen here to order the symmetrical Christoffel indices.";


(* ::Input::GrayLevel[0]:: *)
ChristoffeldSymbol::usage=
"ChristoffeldSymbol[\[DoubleStruckE],\[CapitalGamma],k][expr] expressed the partial derivatives PartialD[Tensor[\[DoubleStruckE],{Void},{i}],j] of basis vectors '\[DoubleStruckE]' on the basis using '\[CapitalGamma]'-noted Christoffel symbols with down indices; k is the dummy index of Einstein summation. We have chosen here to order the symmetrical Christoffel indices.";


(* ::Input::GrayLevel[0]:: *)
LeviCivitaOrder::usage=
"LeviCivitaOrder[e][expr] orders the indices in the Levi-Civita symbol 'e', taking into account the signature of of the permutation, and assign to zero its covariant derivatives.";


(* ::Input::GrayLevel[0]:: *)
FullLeviCivitaExpand::usage=
"FullLeviCivitaExpand[e,g][expr] fully expand in expr product of pairs of Levi Civita tensors with label e in terms of combinaison of products of metric tensors g.";


(* ::Input::GrayLevel[0]:: *)
LeviCivitaSimplify::usage=
"LeviCivitaSimplify[e,\[Epsilon]][g,flavor][expr] transforms the LeviCivita tensors e with down (resp. up) indices into the usual permutation symbol \[Epsilon] multiplied (resp. divided) by the square root on the elementary volume noted for convenience \!\(\*SubscriptBox[\(g\), \(flavor[g]\)]\) defined by the flavor basis. This rule is valid in any space dimension. (see http://mathworld.wolfram.com/permutationTensor.html); LeviCivitaSimplify[e,\[Epsilon]][g][expr] \[Congruent] LeviCivitaSimplify[e,\[Epsilon]][g,Identity][expr].";


(* ::Input::GrayLevel[0]:: *)
CrossProductExpansion::usage=
"CrossProductExpansion[BasisVectors,e][expr] expands three dimensional CrossProducts and Triple Scalar Products of Basis Vectors appearing in expr, into Levi-Civita tensors noted 'e'.";


(* ::Input::GrayLevel[0]:: *)
MetricTripleProduct::usage=
"MetricTripleProduct[basis,metric,LeviCivita][expr] express any scalar triple products into combination of the components using Levi-Civita and metric tensors. Must not be confused with \!\(\*
StyleBox[\"ScalarTripleProduct\",\nFontWeight->\"Bold\"]\) defined in the package Calculus`VectorAnalysis`.";


(* ::Input::GrayLevel[0]:: *)
EvaluateCrossProducts::usage=
"EvaluateCrossProducts[\[DoubleStruckE],g,e,h][expr] evaluates the cross products in expr. \[DoubleStruckE] designates the basis used, e is the Levi-Civita symbol, g the metric tensor, and h a dummy index (not present in expr).";


(* ::Input::GrayLevel[0]:: *)
VolumeSquare::usage=
"VolumeSquare[flavor@g][expr] calculates the square (noted flavor@g) of the elementary volume associated with the metric tensor in the flavor basis. Its is evaluated when TensorValueRules[g] has been previously defined.";


(* ::Input::GrayLevel[0]:: *)
SymbolSpaceDimension::usage=
"SymbolSpaceDimension[\[DoubleStruckE]] gives the dimension of the space in which \[DoubleStruckE] is a basis symbol.";


(* ::Input::GrayLevel[0]:: *)
SetContravariantDisplay::usage="SetContravariantDisplay[\"SemicolonMode\"|\"DelMode\"] will set the display form of unexpanded contravariant derivatives.\nSemicolonMode: a semicolon followed by indices\nDelMode: a \!\(\*
StyleBox[\"\[Del]\",\nFontSize->14]\) with superscript indices.";


(* ::Input::GrayLevel[0]:: *)
SetContravariantD2display::usage="SetContravariantD2display[\"AlternativeMode\"|\"DelMode\"] will set the display form of unexpanded contravariant derivatives.\nAlternativeMode: an Alternative (\!\(\*
StyleBox[\"|\", \"MR\"]\)) followed by indices\nDelMode: a \!\(\*
StyleBox[\"\[Del]\",\nFontSize->14]\) with superscript indices.";


(* ::Input::GrayLevel[0]:: *)
ContravariantD::usage="ContravariantD[tensor, i] represents the contravariant derivative of the tensor with respect to the index i.\n ContravariantD[tensor,{i,j,...}] represents the contravariant derivative with respect to the list of indices. \n Symbols are considered constants.";


(* ::Input::GrayLevel[0]:: *)
ContravariantD2d::usage="ContravariantD2d[tensor, i] represents the 2-dimensional contravariant derivative of the tensor with respect to the index i.\n ContravariantD2d[tensor,{i,j,...}] represents the 2-dimensional contravariant derivative with respect to the list of indices. \n Symbols are considered constants.";


(* ::Input::GrayLevel[0]:: *)
ReleaseHoldD::usage=
"ReleaseHoldD[expr] expands the contravariant derivatives of previously unexpanded terms using an UnnestTensor operation.";


(* ::Input::GrayLevel[0]:: *)
CovariantDSimplify::usage=
"CovariantDSimplify[\[DoubleStruckE],g,e][expr] assign the value zero to the covariant and contravariant derivatives of the basis vectors, the metric tensor g and the Levi-Civita tensor e.
CovariantDSimplify[g][expr] assign the value zero to the covariant or contravariant derivatives of the metric tensor g.
CovariantDSimplify[g,1][expr] commutes the covariant or contravariant derivatives with the metric tensor.";


(* ::Input::GrayLevel[0]:: *)
CovariantDOrdering::usage=
"CovariantDOrdering[expr] orders the indices of covariant derivation in expr, in a standard order. This is only possible in spaces with zero Gaussian curvature, or for second order derivatives of scalar tensors.";


(* ::Input::GrayLevel[0]:: *)
ExpandCovariantD2d::usage=
"ExpandCovariantD2d[{x,\[Delta],g,\[CapitalGamma]},a,permissive:False][expr] will expand first order covariant derivatives of tensors using x as the label for the coordinate positions,\[Delta] as the label for the Kronecker,g as the label for the metric tensor and \[CapitalGamma] as the lavel for Christoffel symbols.The introduced dummy index will be a.
ExpandCovariantD[{x,\[Delta],g,\[CapitalGamma]},{a,b,...},permissive:False][expr] expands higher order covariant derivatives using the list of dummy indices.";


(* ::Input::GrayLevel[0]:: *)
CovariantD2d::usage="CovariantD2d[tensor, i] represents the 2-dimensional covariant derivative of the tensor with respect to the index i.\n CovariantD2d[tensor,{i,j,...}] represents the 2-dimensional covariant derivative with repect to the list of indices. \n Symbols are considered constants.";


(* ::Input::GrayLevel[0]:: *)
TensorComponentsD::usage=
"TensorComponentsD[expr] simplifies in expr the derivation of a tensor component by another component of the same tensor (the components must of course be independent).";


(* ::Input::GrayLevel[0]:: *)
PartialDToDif::usage=
"PartialDToDif[expr] rewrite the partial derivatives in their expanded form present in expr, into the partial derivatives with respect to the index of the coordinates.";


(* ::Input::GrayLevel[0]:: *)
DifToPartialD::usage=
"DifToPartialD[labels][expr] rewrite the partial derivatives with respect to coordinates indices present in , into the partial derivatives in their expanded form parametrized by labels.";


(* ::Input::GrayLevel[0]:: *)
PartialDOrdering::usage=
"PartialDOrdering[expr] orders the indices of partial derivation in expr, in a standard order.";


(* ::Input::GrayLevel[0]:: *)
DifToCov::usage=
"DifToCov[\[CapitalGamma],m][{u,i},j][expr] expresses the partial derivative with respect to index j, of a tensor u present in expr, into a combination of covariant derivative and components of u. The coefficients contain Christoffel symbols \[CapitalGamma] with m as a dummy index.
DifToCov[\[CapitalGamma],m,h][{u,i},{j,k}][expr] do the same kind of expansion for second partial derivatives.";


(* ::Input::GrayLevel[0]:: *)
PartialDSimplify::usage=
"PartialDSimplify[expr] is used in nested partial derivatives to put outside the partial derivatives the factor which do not depend on the coordinate of derivation (see Chapter :Application_Grad_Div_Curl).";


(* ::Input::GrayLevel[0]:: *)
TGrad::usage=
"TGrad[\[DoubleStruckE],flavor@i][f] writes the gradient of a scalar tensor f, in the basis \[DoubleStruckE] with a defined flavor, and the dummy index i.";


(* ::Input::GrayLevel[0]:: *)
TDiv::usage=
"TDiv[\[DoubleStruckE],flavor@i][vector] writes the divergence of a vector field, in the basis \[DoubleStruckE] with a defined flavor, and the dummy index i.";


(* ::Input::GrayLevel[0]:: *)
TCurl::usage=
"TCurl[\[DoubleStruckE],flavor/@{i,j,k},e][vector] writes the curl of a vector field, in the basis \[DoubleStruckE] with a defined flavor, the dummy indices{i,j,k}, and the Levi-Civita symbol e.";


(* ::Input::GrayLevel[0]:: *)
TLaplacian::usage=
"TLaplacian[\[DoubleStruckE],flavor@i][f] writes the laplacian of a scalar tensor f, in the basis \[DoubleStruckE] with a defined flavor, and the dummy index i.";


(* ::Input::GrayLevel[0]:: *)
LaplaceOp::usage=
"LaplaceOp[\[CapitalDelta]][expr] writes all the laplacians in expr, using a \[CapitalDelta] notation.";


(* ::Input::GrayLevel[0]:: *)
FunctionalD::usage=
"FunctionalD[g,flv][L,f] calculates the functional derivative of the functional form L[flv[k],f,\!\(\*SubscriptBox[\(f\), \(flv[k]\)]\),...] with respect to f and its derivatives. g is the metric tensor in a basis with flavor flv.
FunctionalD[g,flv,a,b][L,f] calculates the functional derivative of L, containing covariant derivatives, using a and b as new supplementary indices.";


(* ::Input::GrayLevel[0]:: *)
\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"PartialD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "f_", "]"}], ",", "i_"}], "]"}], ",", 
RowBox[{"{", "Void", "}"}], ",", 
RowBox[{"{", "j_", "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):=\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "f", "]"}], ",", 
RowBox[{"{", 
RowBox[{"i", ",", "j"}], "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)
\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"PartialD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "f_", "]"}], ",", "i_"}], "]"}], ",", 
RowBox[{"{", "j__", "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):=\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "f", "]"}], ",", 
RowBox[{"{", 
RowBox[{"i", ",", "j"}], "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)
\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"PartialD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "f_", "]"}], ",", 
RowBox[{"{", "i_", "}"}]}], "]"}], ",", 
RowBox[{"{", "j__", "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):=\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "f", "]"}], ",", 
RowBox[{"{", 
RowBox[{"i", ",", "j"}], "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)

CovariantD[expr_,{}]:=expr
HoldCovariantD[expr_,{}]:=expr
ContravariantD[expr_,{}]:=expr
HoldContravariantD[expr_,{}]:=expr

CovariantD[expr_,i_]:=Module[{i$},If[MemberQ[BaseIndexQ/@i,True],CovariantD[expr,i$]/.i$->i]]
ContravariantD[expr_,i_]:=Module[{i$},If[MemberQ[BaseIndexQ/@i,True],ContravariantD[expr,i$]/.i$->i]]
(*CovariantD2d[expr_,i_]:=Module[{i$},If[MemberQ[BaseIndexQ/@i,True],CovariantD2d[expr,i$]/.i$->i]]
ContravariantD2d[expr_,i_]:=Module[{i$},If[MemberQ[BaseIndexQ/@i,True],ContravariantD2d[expr,i$]/.i$->i]]*)


(* ::Input::GrayLevel[0]:: *)
Begin["`Private`"]


(* ::Input::GrayLevel[0]:: *)
Tensor[x_Integer]:=x
Tensor[x_Rational]:=x
Tensor[x_Real]:=x
Tensor[x_Complex]:=x

Tensor[t_,{},{}]:=Tensor[t]


(* ::Input::GrayLevel[0]:: *)
HoldFactor[term_][expr_]:=HoldForm[term]Apart[expr/term]


HoldCovariantD[expr_,i_]:=HoldCovariantD[expr,{i}]
HoldCovariantD[T_,i_List]:=Module[{i$},HoldOp[CovariantD][CovariantD[T,i]]]
HoldCovariantD[T_,i_List,j_List]:=Module[{i$},HoldOp[CovariantD][CovariantD[T,i,j]]]
HoldCovariantD[HoldCovariantD[T_,i_],j_]:=HoldCovariantD[T,{i,j}]
\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldCovariantD", "[", 
RowBox[{
RowBox[{"HoldCovariantD", "[", 
RowBox[{"T_", ",", 
RowBox[{"{", 
RowBox[{"i_", ",", "j__"}], "}"}]}], "]"}], ",", 
RowBox[{"{", 
RowBox[{"k_", ",", "l__"}], "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):=HoldCovariantD[T,{i,k}//Flatten,{j,l}//Flatten]

HoldContravariantD[expr_,i_]:=HoldContravariantD[expr,{i}]
HoldContravariantD[T_,i_List]:=Module[{i$},HoldOp[ContravariantD][ContravariantD[T,i]]]
HoldContravariantD[HoldContravariantD[T_,i_],j_]:=HoldContravariantD[T,{i,j}]

HoldCovariantD2d[expr_,i_]:=HoldCovariantD2d[expr,{i}]
HoldCovariantD2d[T_,i_List]:=Module[{i$},HoldOp[CovariantD2d][CovariantD2d[T,i]]]
HoldCovariantD2d[T_,i_List,j_List]:=Module[{i$},HoldOp[CovariantD2d][CovariantD2d[T,i,j]]]
HoldCovariantD2d[HoldCovariantD2d[T_,i_],j_]:=HoldCovariantD2d[T,{i,j}]
\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldCovariantD2d", "[", 
RowBox[{
RowBox[{"HoldCovariantD2d", "[", 
RowBox[{"T_", ",", "i_", ",", "j_"}], "]"}], ",", "k_", ",", "l_"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):=HoldCovariantD2d[T,{i,k}//Flatten,{j,l}//Flatten]

HoldContravariantD2d[expr_,i_]:=HoldContravariantD2d[expr,{i}]
HoldContravariantD2d[T_,i_List]:=Module[{i$},HoldOp[ContravariantD2d][ContravariantD2d[T,i]]]
HoldContravariantD2d[HoldContravariantD2d[T_,i_],j_]:=HoldContravariantD2d[T,{i,j}]

  Unprotect[HoldForm];
HoldForm[CovariantD[CovariantD[expr_,i_List],j_List]]:=HoldForm[CovariantD[expr,{i,j}//Flatten]]
HoldForm[ContravariantD[ContravariantD[expr_,i_List],j_List]]:=HoldForm[ContravariantD[expr,{i,j}//Flatten]]
HoldForm[CovariantD2d[CovariantD2d[expr_,i_List],j_List]]:=HoldForm[CovariantD2d[expr,{i,j}//Flatten]]
HoldForm[ContravariantD2d[ContravariantD2d[expr_,i_List],j_List]]:=HoldForm[ContravariantD2d[expr,{i,j}//Flatten]]
\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldCovariantD", "[", 
RowBox[{
RowBox[{"HoldCovariantD", "[", 
RowBox[{"T_", ",", "i_List", ",", "j_List"}], "]"}], ",", "k_List", ",", "l_List"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):=HoldCovariantD[T,{i,k}//Flatten,{j,l}//Flatten]
\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldCovariantD2d", "[", 
RowBox[{
RowBox[{"HoldCovariantD2d", "[", 
RowBox[{"T_", ",", "i_List", ",", "j_List"}], "]"}], ",", "k_List", ",", "l_List"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):=HoldCovariantD2d[T,{i,k}//Flatten,{j,l}//Flatten]
Protect[HoldForm];


(* ::Input::GrayLevel[0]:: *)
SyntaxInformation[ToFlavorD]={"ArgumentsPattern"->{_,_.}};
ToFlavorD[toflavor_,fromflavor_:Identity][expr_]:=
Module[{switchQ,replacement,diffcase},
If[toflavor=!=Identity\[And]Count[IndexFlavors, {toflavor,_}]==0,Message[Flavor::notflavor,toflavor];Return[expr]];

switchQ[indx_]:=
GetIndexFlavor[indx]===fromflavor\[And]indx=!=Void\[And](Head[RawIndex[indx]]===Integer\[Or]\[Not]BaseIndexQ[indx]);

replacement=
If[switchQ[#],
Which[
fromflavor===Identity,toflavor@#,
True,toflavor@@#],
Identity@#]&;

ToFlavor[toflavor,fromflavor][expr/.
\!\(\*
TagBox[
StyleBox[
RowBox[{"GhostCovariantD2d", "[", 
RowBox[{
RowBox[{"Tensor", "[", "t_", "]"}], ",", "upindices_", ",", "downindices_List"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>CovariantD2d[Tensor[t],replacement/@Flatten[{upindices}],replacement/@Flatten[{downindices}]]/.
GhostCovariantD2d[t_,upindices_,downindices_List]:> CovariantD2d[t,replacement/@Flatten[{upindices}],replacement/@Flatten[{downindices}]]/.
\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldContravariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "t_", "]"}], ",", "upindices_", ",", "downindices_List"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>ContravariantD[Tensor[t],replacement/@Flatten[{upindices}],replacement/@Flatten[{downindices}]]/.
HoldContravariantD[t_,upindices_,downindices_List]:> ContravariantD[t,replacement/@Flatten[{upindices}],replacement/@Flatten[{downindices}]]/.
\!\(\*
TagBox[
StyleBox[
RowBox[{"ContravariantD2d", "[", 
RowBox[{
RowBox[{"Tensor", "[", "t_", "]"}], ",", "upindices_", ",", "downindices_List"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>ContravariantD2d[Tensor[t],replacement/@Flatten[{upindices}],replacement/@Flatten[{downindices}]]/.
ContravariantD2d[t_,upindices_,downindices_List]:> ContravariantD2d[t,replacement/@Flatten[{upindices}],replacement/@Flatten[{downindices}]]
]]


(* ::Input::GrayLevel[0]:: *)
PartialSumHold[i_,j_][expr_]:=If[Length[expr]=!=1,PartialSumTemp[i,j]/@expr,PartialSumTemp[i,j][expr]]

PartialSumTemp[i_,j_][expr_]:=Module[{resu,test,rul},If[FreeQ[expr,HoldForm],PartialSum[i,j][expr],test=(expr/.HoldForm->Identity//First//ParseTermIndices)[[1]];If[Length[test]=!=Length[j],resu=PartialSum[i,j][expr];Goto[end]];rul={Thread[test->i],Thread[test->j]};Off[Part::"pspec"];val0[1]=Table[Clear[u\[Section]];rul[[u\[Section][i\[Section]],i\[Section]]],{i\[Section],Length[j]}];
Do[val0[j\[Section]+1]=Table[#,{u\[Section][j\[Section]],2}]&[val0[j\[Section]]],{j\[Section],Length[j]}];On[Part::"pspec"];resu=Apply[Plus,Flatten[PartialSum[i,j][expr]/.val0[Length[j]+1]]];Clear[val0];Label[end];resu]
]


(* ::Input::GrayLevel[0]:: *)
BasisChange[\[Beta]_,toflavor_][Tensor[T_,list1_,list2_]]:=Module[{ii},Tensor[T,list1,list2]Product[If[list2[[ii]]===Void,Tensor[\[Beta],toflavor[#]&/@{list2[[ii]],list1[[ii]]},{list1[[ii]],list2[[ii]]}],Tensor[\[Beta],{list2[[ii]],list1[[ii]]},toflavor[#]&/@{list1[[ii]],list2[[ii]]}]],{ii,Length[list1]}]]/.{toflavor[Void]->Void,toflavor[f_[i_]]->toflavor[i]}
BasisChange[\[Beta]_,toflavor_][ff_[x_Tensor]]:=ff[BasisChange[\[Beta],toflavor][x]]
BasisChange[\[Beta]_,toflavor_][ff_[x_,y__]]:=ff[BasisChange[\[Beta],toflavor][x],BasisChange[\[Beta],toflavor][y]]
BasisChange[\[Beta]_,toflavor_][x_+y_]:=BasisChange[\[Beta],toflavor][x]+BasisChange[\[Beta],toflavor][y]
BasisChange[\[Beta]_,toflavor_][x_ y_]:=BasisChange[\[Beta],toflavor][x]BasisChange[\[Beta],toflavor][y]
BasisChange[\[Beta]_,toflavor_][a_Symbol]:=a
BasisChange[\[Beta]_,toflavor_][a_Integer]:=a
BasisChange[\[Beta]_,toflavor_][a_Real]:=a
BasisChange[\[Beta]_,toflavor_][a_Rational]:=a
BasisChange[\[Beta]_,toflavor_][a_Complex]:=a


(* ::Input::GrayLevel[0]:: *)
BasisDerivation[\[CapitalGamma]_,m_][{\[DoubleStruckE]_,i_},j_][expr_]:=expr/.{PartialD[Tensor[\[DoubleStruckE],{Void},{i}],j]->Tensor[\[DoubleStruckE],{Void},{m}]*Tensor[\[CapitalGamma],{m,Void,Void},{Void,j,i}],PartialD[Tensor[\[DoubleStruckE],{i},{Void}],j]->-(Tensor[\[DoubleStruckE],{m},{Void}]*Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,m}]),CovariantD[Tensor[\[DoubleStruckE],{Void},{i}],j]:>0,CovariantD[Tensor[\[DoubleStruckE],{i},{Void}],j]:>0}

BasisDerivation[\[CapitalGamma]_,h_,m_][{\[DoubleStruckE]_,i_},{j_,k_}][expr_]:=expr/.{PartialD[Tensor[\[DoubleStruckE],{Void},{i}],{j,k}]->Tensor[\[DoubleStruckE],{Void},{h}]*(Tensor[\[CapitalGamma],{h,Void,Void},{Void,k,m}]*Tensor[\[CapitalGamma],{m,Void,Void},{Void,j,i}]+PartialD[Tensor[\[CapitalGamma],{h,Void,Void},{Void,j,i}],k]),PartialD[Tensor[\[DoubleStruckE],{i},{Void}],{j,k}]->Tensor[\[DoubleStruckE],{h},{Void}]*(Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,m}]*Tensor[\[CapitalGamma],{m,Void,Void},{Void,k,h}]-PartialD[Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,h}],k]),CovariantD[Tensor[\[DoubleStruckE],{Void},{i}],{j}]:>0,CovariantD[Tensor[\[DoubleStruckE],{i},{Void}],{j,k}]:>0}


(* ::Input::GrayLevel[0]:: *)
StandardDownIndices[g_][expr_]:=expr/.Tensor[x_,ups_,downs_]:>Module[{ii,coeffg,indices,updowns1,RawIndexupdowns1,updowns2,siteCov},{siteCov=Table[{ii},{ii,Length[ups]+1,Length[ups]}];
updowns1=({ups,downs}//Transpose);
RawIndexupdowns1=(Table[RawIndex/@(updowns1[[ii]]),{ii,Length[updowns1]}]);
updowns2={};coeffg=1;
Do[Module[{i$},If[Position[RawIndexupdowns1,Void][[ii,2]]==1,updowns2=Append[updowns2,updowns1[[ii]]],(If[Head[updowns1[[ii,1]]]===Symbol,(Flvr=Identity),(Flvr=Head[updowns1[[ii,1]]] )];coeffg=coeffg  Tensor[g,{updowns1[[ii,1]],Flvr@i$},{Void,Void}];updowns2=Append[updowns2,{Void,Flvr@i$}])]],{ii,Length[updowns1]}];
indices=Transpose[updowns2];
coeffg  Tensor[x,indices[[1]],indices[[2]]]}][[1]]/.CovariantD[Tensor[T__],ups_,downs_]:>Module[{ii,coeffg,indices,updowns1,RawIndexupdowns1,updowns2,siteCov},{siteCov=Table[{ii},{ii,Length[ups]+1,Length[ups]}];
updowns1=({ups,downs}//Transpose);
RawIndexupdowns1=(Table[RawIndex/@(updowns1[[ii]]),{ii,Length[updowns1]}]);
updowns2={};coeffg=1;
Do[Module[{i$},If[Position[RawIndexupdowns1,Void][[ii,2]]==1,updowns2=Append[updowns2,updowns1[[ii]]],(If[Head[updowns1[[ii,1]]]===Symbol,(Flvr=Identity),(Flvr=Head[updowns1[[ii,1]]] )];coeffg=coeffg  Tensor[g,{updowns1[[ii,1]],Flvr@i$},{Void,Void}];updowns2=Append[updowns2,{Void,Flvr@i$}])]],{ii,Length[updowns1]}];
indices=Transpose[updowns2];
coeffg  CovariantD[Tensor[T],indices[[1]],indices[[2]]]}][[1]]


(* ::Input::GrayLevel[0]:: *)
CovNotation[cov_][expr_]:=
Module[{seq},expr//.\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", 
RowBox[{"t_", ",", "up1_", ",", "down1_"}], "]"}], ",", "up2_", ",", "down2_"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>(flavor:=First[Union[GetIndexFlavor/@(Delete[{up2,down2},Position[{up2,down2},Void]]//Flatten)]];Tensor[Tensor[t,up1,down1],If[flavor===Identity,DeclareIndexFlavor[{cov,Cov},{cov2d,Cov2d}];seq[cov/@up2,cov/@down2]/.cov[Void]->Void/.seq-> Sequence,learIndexFlavor[{cov,Cov},{cov2d,Cov2d}];seq[flavor/@cov/@up2,flavor/@cov/@down2]/.flavor_@cov@Void->Void/.cov@flavor[x_]->cov[x]/.seq-> Sequence]])//.\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "t_", "]"}], ",", "up2_", ",", "down2_"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>(flavor:=First[Union[GetIndexFlavor/@(Delete[{up2,down2},Position[{up2,down2},Void]]//Flatten)]];Tensor[Tensor[t],If[flavor===Identity,seq[cov/@up2,cov/@down2]/.cov[Void]->Void/.seq-> Sequence,seq[flavor/@cov/@up2,flavor/@cov/@down2]/.flavor_@cov@Void->Void/.cov@flavor[x_]->cov[x]/.seq-> Sequence]])/.Tensor[Tensor[t1_,up1_,down1_],up2_,down2_]->Tensor[t1,Join[up1,up2],Join[down1,down2]]
]

Cov2dNotation[cov_][expr_]:=
Module[{seq},expr//.\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD2d", "[", 
RowBox[{
RowBox[{"Tensor", "[", 
RowBox[{"t_", ",", "up1_", ",", "down1_"}], "]"}], ",", "up2_", ",", "down2_"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>(flavor:=First[Union[GetIndexFlavor/@(Delete[{up2,down2},Position[{up2,down2},Void]]//Flatten)]];Tensor[Tensor[t,up1,down1],If[flavor===Identity,DeclareIndexFlavor[{cov,Cov},{cov2d,Cov2d}];seq[cov/@up2,cov/@down2]/.cov[Void]->Void/.seq-> Sequence,ClearIndexFlavor[{cov,Cov},{cov2d,Cov2d}];seq[flavor/@cov/@up2,flavor/@cov/@down2]/.flavor_@cov@Void->Void/.cov@flavor[x_]->cov[x]/.seq-> Sequence]])//.\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD2d", "[", 
RowBox[{
RowBox[{"Tensor", "[", "t_", "]"}], ",", "up2_", ",", "down2_"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>(flavor:=First[Union[GetIndexFlavor/@(Delete[{up2,down2},Position[{up2,down2},Void]]//Flatten)]];Tensor[Tensor[t],If[flavor===Identity,seq[cov/@up2,cov/@down2]/.cov[Void]->Void/.seq-> Sequence,seq[flavor/@cov/@up2,flavor/@cov/@down2]/.flavor_@cov@Void->Void/.cov@flavor[x_]->cov[x]/.seq-> Sequence]])/.Tensor[Tensor[t1_,up1_,down1_],up2_,down2_]->Tensor[t1,Join[up1,up2],Join[down1,down2]]
]


SymmetricStandardOrder[T_,{ind1_,ind2_}][expr1_+expr2__]:=SymmetricStandardOrder[T,{ind1,ind2}][expr1]+SymmetricStandardOrder[T,{ind1,ind2}][expr2]
SymmetricStandardOrder[T_,{ind1_,ind2_}][expr1_+expr2__+expr3_]:=SymmetricStandardOrder[T,{ind1,ind2}][expr1]+SymmetricStandardOrder[T,{ind1,ind2}][expr2]+SymmetricStandardOrder[T,{ind1,ind2}][expr3]

SymmetricStandardOrder[T_,{ind1_,ind2_}][expr1_*expr2__]:=SymmetricStandardOrder[T,{ind1,ind2}][expr1]SymmetricStandardOrder[T,{ind1,ind2}][expr2]
SymmetricStandardOrder[T_,{ind1_,ind2_}][expr1_*expr2__*expr3_]:=SymmetricStandardOrder[T,{ind1,ind2}][expr1]SymmetricStandardOrder[T,{ind1,ind2}][expr2]SymmetricStandardOrder[T,{ind1,ind2}][expr3]

SymmetricStandardOrder[T_,{ind1_,ind2_}][expr_]:={Off[Take::take,Take::seqs],If[FreeQ[expr,Tensor[T]],If[FreeQ[expr,CovariantD],If[FreeQ[expr,GhostCovariantD2d],SymmetricStandardOrder2[T,{ind1,ind2}][expr],SymmetricStandardOrder12d[T,{ind1,ind2}][expr]],SymmetricStandardOrder13d[T,{ind1,ind2}][expr]],If[FreeQ[expr,GhostCovariantD2d],SymmetricStandardOrder03d[T,{ind1,ind2}][expr],SymmetricStandardOrder02d[T,{ind1,ind2}][expr]]],On[Take::take,Take::seqs]}[[2]]

SymmetricStandardOrder03d[T_,{ind1_,ind2_}][expr_]:=Module[{updn,upsdowns,upsdownsd},
expr/.CovariantD[Tensor[T],upsd_,downsd_]:>(upsdownsd:=Transpose[ReplacePart[updn=Transpose[{upsd,downsd}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1,ind2],SortBy[Take[updn,{ind1,ind2}],Last]]]]]/.\[Section]\[Section]->Void);
CovariantD[Tensor[T],upsdownsd[[1]],upsdownsd[[2]]] 
 ]  
SymmetricStandardOrder02d[T_,{ind1_,ind2_}][expr_]:=Module[{updn,upsdowns,upsdownsd},
expr/.CovariantD2d[Tensor[T],upsd_,downsd_]:>(upsdownsd:=Transpose[ReplacePart[updn=Transpose[{upsd,downsd}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1,ind2],SortBy[Take[updn,{ind1,ind2}],Last]]]]]/.\[Section]\[Section]->Void);
CovariantD2d[Tensor[T],upsdownsd[[1]],upsdownsd[[2]]] 
 ]  

SymmetricStandardOrder13d[T_,{ind1_,ind2_}][expr_]:=Module[{updn,updnd,upsdowns,upsdownsd},
expr/.CovariantD[Tensor[T,ups_,downs_],upsd_,downsd_]:>(lups:=Length[ups];If[ind2>lups,If[ind1>lups,
(upsdowns:={ups,downs};upsdownsd:=Transpose[ReplacePart[updnd=Transpose[{upsd,downsd}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1-lups,ind2-lups],SortBy[Take[updnd,{ind1-lups,ind2-lups}],Last]]]]]/.\[Section]\[Section]->Void),
((upsdownsd:=Transpose[ReplacePart[updnd=Transpose[{upsd,downsd}]/.Void->\[Section]\[Section],Thread[Rule[Range[1,ind2-lups],SortBy[Take[updnd,{1,ind2-lups}],Last]]]]]/.\[Section]\[Section]->Void);(upsdowns:=Transpose[ReplacePart[updn=Transpose[{ups,downs}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1,lups],SortBy[Take[updn,{ind1,lups}],Last]]]]]/.\[Section]\[Section]->Void))],
(upsdownsd:={upsd,downsd};(upsdowns:=Transpose[ReplacePart[updn=Transpose[{ups,downs}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1,ind2],SortBy[Take[updn,{ind1,ind2}],Last]]]]]/.\[Section]\[Section]->Void))];If[((upsdowns//Head)=!=Symbol  ||  (upsdownsd//Head)=!=Symbol),
CovariantD[Tensor[T,upsdowns[[1]],upsdowns[[2]]],upsdownsd[[1]],upsdownsd[[2]]],expr]
   ) ]


SymmetricStandardOrder2[T_,{ind1_,ind2_}][expr_]:=Module[{(*upsdowns*)},
expr/.Tensor[T,ups_,downs_]:>
( (upsdowns:=Transpose[ReplacePart[updn=Transpose[{ups,downs}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1,ind2],SortBy[Take[updn,{ind1,ind2}],Last]]]]]/.\[Section]\[Section]->Void);
If[((upsdowns//Head)=!=Symbol),Tensor[T,upsdowns[[1]],upsdowns[[2]]],expr])
   ] 


SymmetricStandardOrder12d[T_,{ind1_,ind2_}][expr_]:=Module[{updn,updnd,upsdowns,upsdownsd},
expr/.CovariantD2d[Tensor[T,ups_,downs_],upsd_,downsd_]:>(lups:=Length[ups];If[ind2>lups,If[ind1>lups,
(upsdowns:={ups,downs};upsdownsd:=Transpose[ReplacePart[updnd=Transpose[{upsd,downsd}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1-lups,ind2-lups],SortBy[Take[updnd,{ind1-lups,ind2-lups}],Last]]]]]/.\[Section]\[Section]->Void),
((upsdownsd:=Transpose[ReplacePart[updnd=Transpose[{upsd,downsd}]/.Void->\[Section]\[Section],Thread[Rule[Range[1,ind2-lups],SortBy[Take[updnd,{1,ind2-lups}],Last]]]]]/.\[Section]\[Section]->Void);(upsdowns:=Transpose[ReplacePart[updn=Transpose[{ups,downs}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1,lups],SortBy[Take[updn,{ind1,lups}],Last]]]]]/.\[Section]\[Section]->Void))],
(upsdownsd:={upsd,downsd};(upsdowns:=Transpose[ReplacePart[updn=Transpose[{ups,downs}]/.Void->\[Section]\[Section],Thread[Rule[Range[ind1,ind2],SortBy[Take[updn,{ind1,ind2}],Last]]]]]/.\[Section]\[Section]->Void))];If[((upsdowns//Head)=!=Symbol  ||  (upsdownsd//Head)=!=Symbol),
CovariantD2d[Tensor[T,upsdowns[[1]],upsdowns[[2]]],upsdownsd[[1]],upsdownsd[[2]]],expr]
   ) ]


(* ::Input::GrayLevel[0]:: *)
(* Note : a factor a is artificially added to solve a little error in Tensorial  *)

UpDownSwapD[i_][expr_]:=Module[{a,RL,i$},RL=a expr//LowerIndexD[i,i$]//RaiseIndexD[i,i$];If[(expr//LowerIndexD[i,i$])===expr || (expr//RaiseIndexD[i,i$])===expr ,Print["\!\(\*
StyleBox[\"Warning\",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\" \",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\"!\",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\" \",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\"Here\",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\" \",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\"UpDownSwapD\",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\" \",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\"cannot\",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\" \",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\"be\",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\" \",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\"performed\",\nFontColor->RGBColor[0, 0, 1]]\)"],RL/.i$->i/.a->1]]

UpDownSwapD[{i__}][expr_]:=Fold[funct,expr,{i}]//.{funct[term_,j_]:>UpDownSwapD[j][term]}


(* Note : a factor a is artificially added to solve a little error in Tensorial  *)

RaiseIndexD[indices__][expr_]:=Module[{a,expr1},(DefineTensor[cov,cov2d];expr1:=(a expr/.CovariantD[Tensor[var__],i_,j_]:>Tensor[cov@var,i,j]/.CovariantD2d[Tensor[var__],i_,j_]:>Tensor[cov2d@var,i,j])//RaiseIndex[indices]);
(expr1/.Tensor[cov@var__,i_,j_]:> CovariantD[Tensor[var],i,j]/.Tensor[cov2d@var__,i_,j_]:> CovariantD2d[Tensor[var],i,j]//RaiseIndex[indices])/.a->1]

RaiseIndexD[indices__,baseindices_][expr_]:=Module[{a,expr1},(DefineTensor[cov,cov2d];expr1:=(a expr/.CovariantD[Tensor[var__],i_,j_]:>Tensor[cov@var,i,j]/.CovariantD2d[Tensor[var__],i_,j_]:>Tensor[cov2d@var,i,j])//RaiseIndex[indices,baseindices]);
(expr1/.Tensor[cov@var__,i_,j_]:> CovariantD[Tensor[var],i,j]/.Tensor[cov2d@var__,i_,j_]:> CovariantD2d[Tensor[var],i,j]//RaiseIndex[indices,baseindices])/.a->1]


(* Note : a factor a is artificially added to solve a little error in Tensorial  *)

LowerIndexD[indices__][expr_]:=Module[{a,expr1},(DefineTensor[cov,cov2d];expr1:=(a expr/.CovariantD[Tensor[var__],i_,j_]:>Tensor[cov@var,i,j]/.CovariantD2d[Tensor[var__],i_,j_]:>Tensor[cov2d@var,i,j])//LowerIndex[indices]);
(expr1/.Tensor[cov@var__,i_,j_]:> CovariantD[Tensor[var],i,j]/.Tensor[cov2d@var__,i_,j_]:> CovariantD2d[Tensor[var],i,j]//LowerIndex[indices])/.a->1]

LowerIndexD[indices__,baseindices_][expr_]:=Module[{a,expr1},(DefineTensor[cov,cov2d];expr1:=(a expr/.CovariantD[Tensor[var__],i_,j_]:>Tensor[cov@var,i,j]/.CovariantD2d[Tensor[var__],i_,j_]:>Tensor[cov2d@var,i,j])//LowerIndex[indices,baseindices]);
(expr1/.Tensor[cov@var__,i_,j_]:> CovariantD[Tensor[var],i,j]/.Tensor[cov2d@var__,i_,j_]:> CovariantD2d[Tensor[var],i,j]//LowerIndex[indices,baseindices])/.a->1]


(* ::Input::GrayLevel[0]:: *)
KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox["expr_",
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=KroneckerAbsorb[\[Delta]][expr]/;FreeQ[expr,CovariantD]/;FreeQ[expr,CovariantD2d]/;FreeQ[expr,ContravariantD]/;FreeQ[expr,ContravariantD2d]
KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox["expr1_",
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)\!\(\*
TagBox[
StyleBox[
RowBox[{" ", "expr2_"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=KroneckerAbsorbD[\[Delta]][\!\(\*
TagBox[
StyleBox["expr1",
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]KroneckerAbsorbD[\[Delta]][\!\(\*
TagBox[
StyleBox["expr2",
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]

KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldForm", "[", 
RowBox[{"CovariantD", "[", 
RowBox[{"T_", ",", "upindices_", ",", "downindices_"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Module[{KT},HoldForm[CovariantD[KT,upindices,downindices]]/.KT->KroneckerAbsorb[\[Delta]][Expand[T]]]
KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldForm", "[", 
RowBox[{"CovariantD", "[", 
RowBox[{"T_", ",", "downindices_"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Module[{KT},HoldForm[CovariantD[KT,downindices]]/.KT->KroneckerAbsorb[\[Delta]][Expand[T]]]

KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldForm", "[", 
RowBox[{"CovariantD2d", "[", 
RowBox[{"T_", ",", "upindices_", ",", "downindices_"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Module[{KT},HoldForm[CovariantD2d[KT,upindices,downindices]]/.{KT->KroneckerAbsorb[\[Delta]][Expand[T]]}]
KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldForm", "[", 
RowBox[{"CovariantD2d", "[", 
RowBox[{"T_", ",", "downindices_"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Module[{KT},HoldForm[CovariantD2d[KT,downindices]]/.{KT->KroneckerAbsorb[\[Delta]][Expand[T]]}]

KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldForm", "[", 
RowBox[{"ContravariantD", "[", 
RowBox[{"T_", ",", "upindices_", ",", "downindices_"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Module[{KT},HoldForm[ContravariantD[KT,upindices,downindices]]/.{KT->KroneckerAbsorb[\[Delta]][Expand[T]]}]
KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldForm", "[", 
RowBox[{"ContravariantD", "[", 
RowBox[{"T_", ",", "upindices_"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Module[{KT},HoldForm[ContravariantD[KT,upindices]]/.{KT->KroneckerAbsorb[\[Delta]][Expand[T]]}]

KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldForm", "[", 
RowBox[{"ContravariantD2d", "[", 
RowBox[{"T_", ",", "upindices_", ",", "downindices_"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Module[{KT},HoldForm[ContravariantD2d[KT,upindices,downindices]]/.{KT->KroneckerAbsorb[\[Delta]][Expand[T]]}]
KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox[
RowBox[{"HoldForm", "[", 
RowBox[{"ContravariantD2d", "[", 
RowBox[{"T_", ",", "upindices_"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Module[{KT},HoldForm[ContravariantD2d[KT,upindices]]/.{KT->KroneckerAbsorb[\[Delta]][Expand[T]]}]

KroneckerAbsorbD[\[Delta]_][\!\(\*
TagBox[
StyleBox["expr_",
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)]:=Map[KroneckerAbsorbD[\[Delta]],expr]


(* ::Input::GrayLevel[0]:: *)
PartialSumD[i_,j_][term_]:=Module[{ind1,ind2,r1,resu},If[(ind1=ParseTermIndices[term][[1]])=!={},ind2:=Table[i0[n],{n,Length[ind1]}];
r1:=Thread/@Table[Rule[ind2[[ii]],{i,j[[ii]]}],{ii,Length[ind1]}];Off[Part::"pspec",ReplaceAll::"reps"];resu=
(FuncSum[Prepend[Table[{ia[kk],2},{kk,Length[ind1]}],term//IndexChange[{ind1,ind2}//Transpose]/.Table[r1[[jj,ia[jj]]],{jj,Length[ind1]}]]]/.FuncSum[{x__}]:>Sum[x]);On[Part::"pspec",ReplaceAll::"reps"];resu,term]]


(* ::Input::GrayLevel[0]:: *)
MetricRuleD[x_,g_][expr_]:=expr/.CovariantD[Tensor[x,{i_},{Void}],{Void},{j_}]:>Tensor[g,{i,Void},{Void,j}]/.CovariantD[Tensor[x,{i_},{Void}],{j_},{Void}]:>Tensor[g,{i,j},{Void,Void}]/.CovariantD[Tensor[x,{Void},{i_}],{Void},{j_}]:>Tensor[g,{Void,Void},{i,j}]/.CovariantD[Tensor[x,{Void},{i_}],{j_},{Void}]:>Tensor[g,{Void,j},{i,Void}]//Kronecker[g]


(* ::Input::GrayLevel[0]:: *)
Kronecker[\[Delta]_][expr_]:=expr/.Tensor[\[Delta],{i_,Void},{Void,j_}]/;(BaseIndexQ[i]&&BaseIndexQ[j]):> If[Head[i]===Head[j],KroneckerDelta[RawIndex[i],RawIndex[j]],Tensor[\[Delta],{i,Void},{Void,j}]]/.Tensor[\[Delta],{Void,i_},{j_,Void}]/;(BaseIndexQ[i]&&BaseIndexQ[j]):> If[Head[i]===Head[j],KroneckerDelta[RawIndex[i],RawIndex[j]],Tensor[\[Delta],{Void,i},{j,Void}]]


(* ::Input::GrayLevel[0]:: *)
ChristoffeluSymbol[\[DoubleStruckE]_,\[CapitalGamma]_,k_][expr_]:=expr/.{PartialD[Tensor[\[DoubleStruckE],{Void},{i_}],j_]:>If[Head[i]===Symbol,Tensor[\[DoubleStruckE],{Void},{k}]Tensor[\[CapitalGamma],{k,Void,Void},{Void,i,j}],Tensor[\[DoubleStruckE],{Void},{Head[i]@k}]Tensor[\[CapitalGamma],{Head[i]@k,Void,Void},{Void,i,j}]],PartialD[Tensor[\[DoubleStruckE],{i_},{Void}],j_]:>If[Head[i]===Symbol,-Tensor[\[DoubleStruckE],{k},{Void}]Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,k}],-Tensor[\[DoubleStruckE],{Head[i]@k},{Void}]Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,Head[i]@k}]]};


(* ::Input::GrayLevel[0]:: *)
ChristoffeldSymbol[\[DoubleStruckE]_,\[CapitalGamma]_,k_][expr_]:=expr/.{PartialD[Tensor[\[DoubleStruckE],{Void},{i_}],j_]:>If[Head[i]===Symbol,Tensor[\[DoubleStruckE],{k},{Void}]Tensor[\[CapitalGamma],{Void,Void,Void},{k,i,j}],Tensor[\[DoubleStruckE],{Head[i]@k},{Void}]Tensor[\[CapitalGamma],{Void,Void,Void},{Head[i]@k,i,j}]]};


(* ::Input::GrayLevel[0]:: *)
LeviCivitaOrder[e_][expr_]:=expr/.{Tensor[e,{i_,j_,k_},{Void,Void,Void}]:>Signature[{i,j,k}]Tensor[e,Sort[{i,j,k}],{Void,Void,Void}],Tensor[e,{Void,Void,Void},{i_,j_,k_}]:>Signature[{i,j,k}]Tensor[e,{Void,Void,Void},Sort[{i,j,k}]],Tensor[e,{i_,j_,k_,Void},{Void,Void,Void,Cov[h_]}]:>0,Tensor[e,{Void,Void,Void,Void},{i_,j_,k_,Cov[h_]}]:>0,Tensor[e,{i_,j_,k_,Void,__},{Void,Void,Void,Cov[h_],__}]:>0,Tensor[e,{Void,Void,Void,Void,__},{i_,j_,k_,Cov[h_],__}]:>0}/.{Tensor[e,{i_,j_},{Void,Void}]:>Signature[{i,j}]Tensor[e,Sort[{i,j}],{Void,Void}],Tensor[e,{Void,Void},{i_,j_}]:>Signature[{i,j}]Tensor[e,{Void,Void},Sort[{i,j}]],Tensor[e,{i_,j_,Void},{Void,Void,Cov[h_]}]:>0,Tensor[e,{Void,Void,Void},{i_,j_,Cov[h_]}]:>0,Tensor[e,{i_,j_,Void,__},{Void,Void,Cov[h_],__}]:>0,Tensor[e,{Void,Void,Void,__},{i_,j_,Cov[h_],__}]:>0}


(* ::Input::GrayLevel[0]:: *)
FullLeviCivitaExpand[e_,g_][expr_]:=expr//.Module[{w$,i$,j$,perm2,sign2,perm2Up,perm2Down},Tensor[e,ups1_,downs1_]Tensor[e,ups2_,downs2_]:>If[Length[ups1]==Length[ups2],(perm2:=Permutations[ups2+w$ downs2];sign0:=Signature[ups2+w$ downs2];sign2:=Signature/@perm2;
perm2Up:=perm2/.w$->0;
perm2Down:=perm2-perm2Up/.w$->1;sign0 Sum[sign2[[j$]]Product[Tensor[g,{ups1[[i$]],perm2Up[[j$,i$]]},{downs1[[i$]],perm2Down[[j$,i$]]}],{i$,Length[ups1]}],{j$,Length[perm2]}]//MetricSimplify[g]//Simplify)]]


(* ::Input::GrayLevel[0]:: *)
LeviCivitaSimplify[e_,\[Epsilon]_][g_,flavor_][expr_]:=expr/.Tensor[e,list1_,list2_]:>If[list1===Table[Void,{ii,Length[list1]}],Tensor[g,{Void},{flavor[g]}]*Tensor[\[Epsilon],list1,list2],If[list2===Table[Void,{ii,Length[list2]}],Tensor[g,{flavor[g]},{Void}]*Tensor[\[Epsilon],list1,list2],Print["Error ! Base Indices? "]]]/.{Tensor[g,{Void},{flavor[g]}]:>Sqrt[Tensor[flavor[g]]],Tensor[g,{flavor[g]},{Void}]:>1/Sqrt[Tensor[flavor[g]]],PartialD[Tensor[g,{Void},{flavor[g]}],flavor[m_]]->(1/(2*Sqrt[Tensor[flavor[g]]]))*PartialD[Tensor[flavor[g]],flavor[m]],PartialD[Tensor[g,{flavor[g]},{Void}],flavor[m_]]->((-(1/2))*PartialD[Tensor[flavor[g]],flavor[m]])/Tensor[flavor[g]]^(3/2)}
LeviCivitaSimplify[e_,\[Epsilon]_][g_][expr_]:=LeviCivitaSimplify[e,\[Epsilon]][g,Identity][expr]


(* ::Input::GrayLevel[0]:: *)
CrossProductExpansion[\[DoubleStruckE]_,e_,h_][expr_]:=expr/.
{Tensor[\[DoubleStruckE],{i_},{Void}]\[Cross]Tensor[\[DoubleStruckE],{j_},{Void}].Tensor[\[DoubleStruckE],{k_},{Void}]:>Tensor[e,{i,j,k},{Void,Void,Void}],Tensor[\[DoubleStruckE],{i_},{Void}]\[Cross]Tensor[\[DoubleStruckE],{j_},{Void}].Tensor[\[DoubleStruckE],{Void},{k_}]:>Tensor[e,{i,j,Void},{Void,Void,k}],Tensor[\[DoubleStruckE],{i_},{Void}]\[Cross]Tensor[\[DoubleStruckE],{Void},{j_}].Tensor[\[DoubleStruckE],{k_},{Void}]:>Tensor[e,{i,Void,k},{Void,j,Void}],Tensor[\[DoubleStruckE],{i_},{Void}]\[Cross]Tensor[\[DoubleStruckE],{Void},{j_}].Tensor[\[DoubleStruckE],{Void},{k_}]:>Tensor[e,{i,Void,Void},{Void,j,k}],Tensor[\[DoubleStruckE],{Void},{i_}]\[Cross]Tensor[\[DoubleStruckE],{j_},{Void}].Tensor[\[DoubleStruckE],{Void},{k_}]:>Tensor[e,{Void,j,Void},{i,Void,k}],Tensor[\[DoubleStruckE],{Void},{i_}]\[Cross]Tensor[\[DoubleStruckE],{j_},{Void}].Tensor[\[DoubleStruckE],{k_},{Void}]:>Tensor[e,{Void,j,k},{i,Void,Void}],Tensor[\[DoubleStruckE],{Void},{i_}]\[Cross]Tensor[\[DoubleStruckE],{Void},{j_}].Tensor[\[DoubleStruckE],{k_},{Void}]:>Tensor[e,{Void,Void,k},{i,j,Void}],Tensor[\[DoubleStruckE],{Void},{i_}]\[Cross]Tensor[\[DoubleStruckE],{Void},{j_}].Tensor[\[DoubleStruckE],{Void},{k_}]:>Tensor[e,{Void,Void,Void},{i,j,k}],

Tensor[\[DoubleStruckE],{Void},{i_}]\[Cross]Tensor[\[DoubleStruckE],{j_},{Void}]:>If[Head[i]=!=Symbol ,If[Head[i]===Head[j],Tensor[e,{Void,j,Void},{i,Void,Head[i][h]}] Tensor[\[DoubleStruckE],{Head[i][h]},{Void}],Print[Head[i],"Error : not in the same flavor"]],Tensor[e,{Void,j,Void},{i,Void,h}] Tensor[\[DoubleStruckE],{h},{Void}]],
Tensor[\[DoubleStruckE],{i_},{Void}]\[Cross]Tensor[\[DoubleStruckE],{Void},{j_}]:>If[Head[i]=!=Symbol,If[Head[i]===Head[j],Tensor[e,{i,Void,Head[i][h]},{Void,j,Void}] Tensor[\[DoubleStruckE],{Void},{Head[i][h]}],Print[Head[i],"Error : not in the same flavor"]],Tensor[e,{i,Void,h},{Void,j,Void}] Tensor[\[DoubleStruckE],{Void},{h}]],
Tensor[\[DoubleStruckE],{Void},{i_}]\[Cross]Tensor[\[DoubleStruckE],{Void},{j_}]:>If[Head[i]=!=Symbol,If[Head[i]===Head[j],Tensor[e,{Void,Void,Void},{i,j,Head[i][h]}] Tensor[\[DoubleStruckE],{Head[i][h]},{Void}],Print[Head[i],"Error : not in the same flavor"]],Tensor[e,{Void,Void,Void},{i,j,h}] Tensor[\[DoubleStruckE],{h},{Void}]],
Tensor[\[DoubleStruckE],{i_},{Void}]\[Cross]Tensor[\[DoubleStruckE],{j_},{Void}]:>If[Head[i]=!=Symbol,If[Head[i]===Head[j],Tensor[e,{i,j,Head[i][h]},{Void,Void,Void}] Tensor[\[DoubleStruckE],{Void},{Head[i][h]}],Print[Head[i],"Error : not in the same flavor"]],Tensor[e,{i,j,h},{Void,Void,Void}] Tensor[\[DoubleStruckE],{Void},{h}]]}/.{Tensor[e,{i_,j_,k_},{Void,Void,Void}]:>0/;((i==j) || (j==k) || (k==i)),Tensor[e,{Void,Void,Void},{i_,j_,k_}]:>0/;((i==j) || (j==k) || (k==i))}


(* ::Input::GrayLevel[0]:: *)
MetricTripleProduct[basis_,metric_,LeviCivita_,k_][expr_]:=(Print["\!\(\*
StyleBox[\"Warning\",\nFontColor->RGBColor[0, 0, 1]]\) : be sure that index '", k,"' introduced above, does not appear in ",expr ];expr//LinearBreakout[Cross][Tensor[basis,{_},{Void}],Tensor[basis,{Void},{_}]])//CrossProductExpansion[basis,LeviCivita,k]//LinearBreakout[Dot][Tensor[basis,{_},{Void}],Tensor[basis,{Void},{_}]]//EvaluateDotProducts[basis,metric,False]//MetricSimplify[metric]


(* ::Input::GrayLevel[0]:: *)
EvaluateCrossProducts[\[DoubleStruckE]_,e_,g_,h_][expr_]:=(expr//LinearBreakout[Cross][Tensor[\[DoubleStruckE],{_},{Void}],Tensor[\[DoubleStruckE],{Void},{_}]]//CrossProductExpansion[\[DoubleStruckE],e,h]//FullLeviCivitaExpand[e,g]//MetricSimplify[g])/.{Tensor[g,{i_,Void},{Void,i_}]:>NDim,Tensor[g,{Void,i_},{i_,Void}]:>NDim}


(* ::Input::GrayLevel[0]:: *)
VolumeSquare[flavor_@g_][expr_]:=expr/.Tensor[flavor@g]:>( Det[Tensor[g,{Void,Void},{flavor@i,flavor@j}]//ArrayExpansion[flavor@i,flavor@j]]/.TensorValueRules[g])
VolumeSquare[g_][expr_]:=expr/.Tensor[g]:> (Det[Tensor[g,{Void,Void},{i,j}]//ArrayExpansion[i,j]]/.TensorValueRules[g])


(* ::Input::GrayLevel[0]:: *)
ReleaseHoldD[expr_]:=(SetScalarSingleCovariantD[True];SetScalarSingleCovariantD[False];(((expr//UnnestTensor//ReleaseHold)/.{GhostCovariantD2d:>CovariantD2d}/.{CovariantD:>CovariantDTemp,HoldContravariantD:>CovariantD}/.{CovariantDTemp:>CovariantD,CovariantD:>HoldContravariantD})/.{HoldContravariantD2d:>CovariantD,CovariantD:> CovariantDTemp}/.{CovariantDTemp:>CovariantD,CovariantD:>HoldContravariantD2d}/.
GhostCovariantD2d->CovariantD2d))


ContravariantD[expr_,i_]:=ContravariantD[expr,{i}]
ContravariantD[T_,i_List]:=Module[{i$},CovariantD[T,{i}//Flatten,Table[Void,{i$,Length[i//Flatten]}]]]


SetScalarSingleCovariantD[on_: True]:=If[on,(CovariantD[Tensor[T_],j_]:=PartialD[Tensor[T],j]);CovariantD[Tensor[T_],{Void},{j_}]:=PartialD[Tensor[T],j];scalarcovariant=True;,If[scalarcovariant,CovariantD[Tensor[T_],{Void},{j_}]=.;scalarcovariant=False;]]
CovariantD[Tensor[T_],j_]/;!VectorQ[j]\[And]FreeQ[T,Tensor]:=CovariantD[Tensor[T],j]/.CovariantD->PartialD;
scalarcovariant=True;

CovariantD[Tensor[T_],{Void},{j_}]:=PartialD[Tensor[T],j];
scalarcovariant=True;

CovariantD[Tensor[T_],{j_}]:=Print["5",on];CovariantD[Tensor[T],j];


(* ::Input::GrayLevel[0]:: *)
CovariantDSimplify[\[DoubleStruckE]_,g_,e_][expr_]:=(If[SymbolSpaceDimension[\[DoubleStruckE]]=!=NDim,Print["\!\(\*
StyleBox[\"CovariantDSimplify\",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\" \",\nFontColor->RGBColor[0, 0, 1]]\)\!\(\*
StyleBox[\"warning\",\nFontColor->RGBColor[0, 0, 1]]\) : the present dimension is NDim = ",NDim,": check if the basis or the metric tensor are correct!"]];expr/.{CovariantD[Tensor[\[DoubleStruckE],i_,j_],k_,l_]:>0,CovariantD[Tensor[g,i_,j_],k_,l_]:>0,CovariantD[Tensor[e,i_,j_],k_,l_]:>0,HoldContravariantD[Tensor[\[DoubleStruckE],i_,j_],k_,l_]:>0,ContravariantD[Tensor[g,i_,j_],k_,l_]:>0,ContravariantD[Tensor[e,i_,j_],k_,l_]:>0}/.{CovariantD2d[Tensor[\[DoubleStruckE],i_,j_],k_,l_]:>0,CovariantD2d[Tensor[g,i_,j_],k_,l_]:>0,CovariantD2d[Tensor[e,i_,j_],k_,l_]:>0,ContravariantD2d[Tensor[\[DoubleStruckE],i_,j_],k_,l_]:>0,ContravariantD2d[Tensor[g,i_,j_],k_,l_]:>0,ContravariantD2d[Tensor[e,i_,j_],k_,l_]:>0}/.HoldOp[CovariantD][CovariantD[t_, w__]]Tensor[g,up_,down_]:>HoldOp[CovariantD][CovariantD[t Tensor[g,up,down], w]]/.HoldOp[ContravariantD][ContravariantD[t_, w__]]Tensor[g,up_,down_]:>HoldOp[ContravariantD][ContravariantD[t Tensor[g,up,down], w]]/.HoldOp[CovariantD2d][CovariantD2d[t_, w__]]Tensor[g,up_,down_]:>HoldOp[CovariantD2d][CovariantD2d[t Tensor[g,up,down], w]]/.HoldOp[ContravariantD2d][ContravariantD2d[t_, w__]]Tensor[g,up_,down_]:>HoldOp[ContravariantD2d][ContravariantD2d[t Tensor[g,up,down], w]])

CovariantDSimplify[g_][expr_]:=Module[{a$,b$},SymbolSpaceDimension[a$]=NDim;CovariantDSimplify[a$,g,b$][expr]]


(* ::Input::GrayLevel[0]:: *)
CovariantDOrdering[expr_]:=(If[FreeQ[%,CovariantD[_,__]]==False,Print["We can use \!\(\*
StyleBox[\"CovariantDOrdering\",\nFontWeight->\"Bold\",\nFontColor->RGBColor[0, 0, 1]]\) if the space has a zero Gaussian curvature !"]];expr//.{CovariantD[Tensor[T_],{i__},{j__}]:> 
CovariantD[Tensor[T],Transpose[Sort[Transpose[{{i}//Flatten,{j}//Flatten}]]][[1]],Transpose[Sort[Transpose[{{i}//Flatten,{j}//Flatten}]]][[2]]],CovariantD[Tensor[t_, {u1___}, {v1___}],{i___},{j___}]:>CovariantD[Tensor[t, {u1}, {v1}],Transpose[Sort[Transpose[{{i}//Flatten,{j}//Flatten}]]][[1]],Transpose[Sort[Transpose[{{i}//Flatten,{j}//Flatten}]]][[2]]]
}//.{CovariantD2d[Tensor[T_],{i__},{j__}]:> 
CovariantD2d[Tensor[T],Transpose[Sort[Transpose[{{i}//Flatten,{j}//Flatten}]]][[1]],Transpose[Sort[Transpose[{{i}//Flatten,{j}//Flatten}]]][[2]]],CovariantD2d[Tensor[t_, {u1___}, {v1___}],{i___},{j___}]:>CovariantD2d[Tensor[t, {u1}, {v1}],Transpose[Sort[Transpose[{{i}//Flatten,{j}//Flatten}]]][[1]],Transpose[Sort[Transpose[{{i}//Flatten,{j}//Flatten}]]][[2]]]
})


(* ::Input::GrayLevel[0]:: *)
MakeBoxes[HoldForm[CovariantD[t_, upindices__,downindices__]], form : StandardForm | TraditionalForm] :=
   Module[{downindx = Flatten[{downindices}]/.Void-> Style[" ",ShowContents->False],upindx = Flatten[{upindices}]/.Void-> Style[" ",ShowContents->False]},
    If[MatchQ[t, Tensor[_, _, _] | Tensor[_Symbol]],
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[MakeBoxes[t, form], RowBox[{CovSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}] ,RowBox[{ToBoxes[Invisible[CovSym],form], RowBox@(MakeBoxes[#, form] & /@ upindx)}]], HoldForm[CovariantD[t, upindices,downindices]]},
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[RowBox[{"(", MakeBoxes[t, form], ")"}], RowBox[{CovSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}],RowBox[{ToBoxes[Invisible[CovSym],form], RowBox@(MakeBoxes[#, form] & /@ upindx)}]], HoldForm[CovariantD[t, upindices,downindices]]}]]


(* ::Input::GrayLevel[0]:: *)
MakeBoxes[HoldForm[CovariantD2d[t_, upindices__,downindices__]], form : StandardForm | TraditionalForm] :=
   Module[{downindx = Flatten[{downindices}]/.Void-> Style[" ",ShowContents->False],upindx = Flatten[{upindices}]/.Void-> Style[" ",ShowContents->False]},
    If[MatchQ[t, Tensor[_, _, _] | Tensor[_Symbol]],
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[MakeBoxes[t, form], RowBox[{Cov2dSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}] ,RowBox[{ToBoxes[Invisible[Cov2dSym],form], RowBox@(MakeBoxes[#, form] & /@ upindx)}]], HoldForm[CovariantD2d[t, upindices,downindices]]},
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[RowBox[{"(", MakeBoxes[t, form], ")"}], RowBox[{Cov2dSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}],RowBox[{ToBoxes[Invisible[Cov2dSym],form], RowBox@(MakeBoxes[#, form] & /@ upindx)}]], HoldForm[CovariantD2d[t, upindices,downindices]]}]]


(* ::Input::GrayLevel[0]:: *)
MakeBoxes[GhostCovariantD2d[t_, upindices__,downindices__], form : StandardForm | TraditionalForm] :=
   Module[{downindx = Flatten[{downindices}]/.Void-> Style[" ",ShowContents->False],upindx = Flatten[{upindices}]/.Void-> Style[" ",ShowContents->False]},
    If[MatchQ[t, Tensor[_, _, _] | Tensor[_Symbol]],
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[MakeBoxes[t, form], RowBox[{Cov2dSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}] ,RowBox[{ToBoxes[Invisible[Cov2dSym],form], RowBox@(MakeBoxes[#, form] & /@ upindx)}]], GhostCovariantD2d[t, upindices,downindices]},
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[RowBox[{"(", MakeBoxes[t, form], ")"}], RowBox[{Cov2dSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}],RowBox[{ToBoxes[Invisible[Cov2dSym],form], RowBox@(MakeBoxes[#, form] & /@ upindx)}]], GhostCovariantD2d[t, upindices,downindices]}]]


(* ::Input::GrayLevel[0]:: *)
MakeBoxes[ContravariantD2d[t_,indices_],form:StandardForm|TraditionalForm]:=
Module[{windx=Flatten[{indices}]},
If[MatchQ[t, Tensor[_,_,_]|Tensor[f_Symbol]],
InterpretationBox[#1,#2,Editable->False]&@@{SuperscriptBox[MakeBoxes[t,form],RowBox[{Cov2dSym,RowBox@(MakeBoxes[#,form]&/@windx)}]],HoldContravariantD2d[t,indices]},
InterpretationBox[#1,#2,Editable->False]&@@{SuperscriptBox[RowBox[{"(",MakeBoxes[t,form],")"}],RowBox[{Cov2dSym,RowBox@(MakeBoxes[#,form]&/@windx)}]],HoldContravariantD2d[t,indices]}]]


(* ::Input::GrayLevel[0]:: *)
MakeBoxes[HoldForm[CovariantD[t_,downindices__]], form : StandardForm | TraditionalForm] :=
   Module[{downindx = Flatten[{downindices}]/.Void-> Style[" ",ShowContents->False]},
    If[MatchQ[t, Tensor[_, _, _] | Tensor[_Symbol]],
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[MakeBoxes[t, form], RowBox[{CovSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}] ,RowBox[{ToBoxes[Invisible[CovSym],form]}]], HoldForm[CovariantD[t, downindices]]},
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[RowBox[{"(", MakeBoxes[t, form], ")"}], RowBox[{CovSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}],RowBox[{ToBoxes[Invisible[CovSym],form]}]], HoldForm[CovariantD[t,downindices]]}]]


(* ::Input::GrayLevel[0]:: *)
MakeBoxes[ContravariantD[t_,indices_],form:StandardForm|TraditionalForm]:=
Module[{windx=Flatten[{indices}]},
If[MatchQ[t, Tensor[_,_,_]|Tensor[f_Symbol]],
InterpretationBox[#1,#2,Editable->False]&@@{SuperscriptBox[MakeBoxes[t,form],RowBox[{CovSym,RowBox@(MakeBoxes[#,form]&/@windx)}]],HoldContravariantD[t,indices]},
InterpretationBox[#1,#2,Editable->False]&@@{SuperscriptBox[RowBox[{"(",MakeBoxes[t,form],")"}],RowBox[{CovSym,RowBox@(MakeBoxes[#,form]&/@windx)}]],HoldContravariantD[t,indices]}]]


(* ::Input::GrayLevel[0]:: *)
MakeBoxes[HoldForm[CovariantD2d[t_,downindices__]], form : StandardForm | TraditionalForm] :=
   Module[{downindx = Flatten[{downindices}]/.Void-> Style[" ",ShowContents->False]},
    If[MatchQ[t, Tensor[_, _, _] | Tensor[_Symbol]],
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[MakeBoxes[t, form], RowBox[{Cov2dSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}] ,RowBox[{ToBoxes[Invisible[CovSym],form]}]], HoldForm[CovariantD2d[t, downindices]]},
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[RowBox[{"(", MakeBoxes[t, form], ")"}], RowBox[{Cov2dSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}],RowBox[{ToBoxes[Invisible[CovSym],form]}]], HoldForm[CovariantD2d[t,downindices]]}]]


(* ::Input::GrayLevel[0]:: *)
MakeBoxes[GhostCovariantD2d[t_,downindices__], form : StandardForm | TraditionalForm] :=
   Module[{downindx = Flatten[{downindices}]/.Void-> Style[" ",ShowContents->False]},
    If[MatchQ[t, Tensor[_, _, _] | Tensor[_Symbol]],
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[MakeBoxes[t, form], RowBox[{Cov2dSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}] ,RowBox[{ToBoxes[Invisible[CovSym],form]}]], HoldForm[CovariantD2d[t, downindices]]},
     InterpretationBox[#1, #2, Editable -> False] & @@ {SubsuperscriptBox[RowBox[{"(", MakeBoxes[t, form], ")"}], RowBox[{Cov2dSym, RowBox@(MakeBoxes[#, form] & /@ downindx)}],RowBox[{ToBoxes[Invisible[CovSym],form]}]], GhostCovariantD2d[t,downindices]}]]


(* ::Input::GrayLevel[0]:: *)
ExpandCovariantD2d[{x_,\[Delta]_,g_,\[CapitalGamma]_},a_][expr_]:=If[SymbolSpaceDimension[\[CapitalGamma]]==2 && SymbolSpaceDimension[g]==2,ExpandCovariantD[{x,\[Delta],g,\[CapitalGamma]},a][expr/.{GhostCovariantD2d:>CovariantD,CovariantD:>CovariantDTemp}]/.{CovariantDTemp:>CovariantD,CovariantD:>GhostCovariantD2d},expr]

ExpandCovariantD2d[{x_,\[Delta]_,g_,\[CapitalGamma]_},a_,TrueFalse_][expr_]:=If[SymbolSpaceDimension[\[CapitalGamma]]==2 && SymbolSpaceDimension[g]==2,ExpandCovariantD[{x,\[Delta],g,\[CapitalGamma]},a,TrueFalse][expr/.{GhostCovariantD2d:>CovariantD,CovariantD:>CovariantDTemp}]/.{CovariantDTemp:>CovariantD,CovariantD:>GhostCovariantD2d},expr]


(* ::Input::GrayLevel[0]:: *)
CovariantD2d[T_,k__]:=CovariantD[T/.{CovariantD2d->CovariantD,CovariantD->CovariantDTemp},k]/.{CovariantD->GhostCovariantD2d,CovariantDTemp->CovariantD}
\!\(\*
TagBox[
StyleBox[
RowBox[{"GhostCovariantD2d", "[", 
RowBox[{
RowBox[{"GhostCovariantD2d", "[", 
RowBox[{"T_", ",", "i_", ",", "j_"}], "]"}], ",", "k_", ",", "l_"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):=GhostCovariantD2d[T,{i,k}//Flatten,{j,l}//Flatten]


(* ::Input::GrayLevel[0]:: *)
ContravariantD2d[T_,k__]:=ContravariantD[T/.{CovariantD2d->CovariantD,CovariantD->CovariantDTemp},k]/.{CovariantD->GhostCovariantD2d,CovariantDTemp->CovariantD}


(* ::Input::GrayLevel[0]:: *)
TensorComponentsD[g_][expr_]:=expr/.{PartialD[Tensor[t_,{ups1__},{downs1__}],Tensor[t_,{ups2__},{downs2__}]]:>If[Length[{ups1}]==Length[{ups2}]&&Length[{downs1}]==Length[{downs2}],Product[Tensor[g,{{ups1}[[i$]],{downs2}[[i$]]},{Void,Void}],{i$,Length[{ups1}]}] Product[Tensor[g,{{ups1}[[i$]],Void},{Void,{ups2}[[i$]]}],{i$,Length[{ups1}]}]*Product[Tensor[g,{{downs2}[[i$]],Void},{Void,{downs1}[[i$]]}],{i$,Length[{ups1}]}]Product[Tensor[g,{Void,Void},{{ups2}[[i$]],{downs1}[[i$]]}],{i$,Length[{ups1}]}]/.{Tensor[g,{i_,Void},{Void,Void}]->1,Tensor[g,{Void,i_},{Void,Void}]->1,Tensor[g,{Void,Void},{i_,Void}]->1,Tensor[g,{Void,Void},{Void,i_}]->1},0],PartialD[{x_,\[Delta]_,g,\[CapitalGamma]_}][Tensor[t_,{ups1__},{downs1__}],Tensor[t_,{ups2__},{downs2__}]]:>If[Length[{ups1}]==Length[{ups2}]&&Length[{downs1}]==Length[{downs2}],Product[Tensor[g,{{ups1}[[i$]],{downs2}[[i$]]},{Void,Void}],{i$,Length[{ups1}]}]Product[Tensor[g,{{ups1}[[i$]],Void},{Void,{ups2}[[i$]]}],{i$,Length[{ups1}]}]*Product[Tensor[g,{{downs2}[[i$]],Void},{Void,{downs1}[[i$]]}],{i$,Length[{ups1}]}]Product[Tensor[g,{Void,Void},{{ups2}[[i$]],{downs1}[[i$]]}],{i$,Length[{ups1}]}]/.{Tensor[g,{i_,Void},{Void,Void}]->1,Tensor[g,{Void,i_},{Void,Void}]->1,Tensor[g,{Void,Void},{i_,Void}]->1,Tensor[g,{Void,Void},{Void,i_}]->1},0]}


(* ::Input::GrayLevel[0]:: *)
PartialDToDif[expr_]:=Module[{ii},expr/.PartialD[{x_,\[Delta]_,g_,\[CapitalGamma]_}][Tensor[t__],i_]:>(ii= (i/.Tensor[x,{j_},{Void}]->j);PartialD[Tensor[t],ii])]


(* ::Input::GrayLevel[0]:: *)
DifToPartialD[{x_,\[Delta]_,g_,\[CapitalGamma]_}][expr_]:=Module[{PD},expr/.PartialD->PD/.PD[y_,var_List]:>PartialD[{x,\[Delta],g,\[CapitalGamma]}][y,Tensor[x,{#},{Void}]&/@var]/.PD[y_,var_]:>PartialD[{x,\[Delta],g,\[CapitalGamma]}][y,Tensor[x,{var},{Void}]]]


(* ::Input::GrayLevel[0]:: *)
PartialDOrdering[expr_]:=expr//.{
PartialD[Tensor[T_],{i___}]:> 
PartialD[Tensor[T],Sort[{i}//Flatten]],PartialD[Tensor[t_, {u1___}, {v1___}],{i___}]:>
PartialD[Tensor[t, {u1}, {v1}],Sort[{i}//Flatten]]
}


(* ::Input::GrayLevel[0]:: *)
DifToCov[\[CapitalGamma]_,m_][{u1_,i_},j_][expr_]:=expr/.{PartialD[Tensor[u1,{i},{Void}],j]:>CovariantD[Tensor[u1,{i},{Void}],j]-Tensor[u1,{m},{Void}]Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,m}],PartialD[Tensor[u1,{Void},{i}],j]:>CovariantD[Tensor[u1,{Void},{i}],j]+Tensor[u1,{Void},{m}]Tensor[\[CapitalGamma],{m,Void,Void},{Void,i,j}]}

DifToCov[\[CapitalGamma]_,m_,h_][{u1_,i_},{j_,k_}][expr_]:=expr/.{PartialD[Tensor[u1,{Void},{i}],{j,k}]->CovariantD[Tensor[u1,{Void},{i}],{j,k}]+CovariantD[Tensor[u1,{Void},{h}],j]*Tensor[\[CapitalGamma],{h,Void,Void},{Void,i,k}]+CovariantD[Tensor[u1,{Void},{i}],h]*Tensor[\[CapitalGamma],{h,Void,Void},{Void,j,k}]+CovariantD[Tensor[u1,{Void},{m}],k]*Tensor[\[CapitalGamma],{m,Void,Void},{Void,i,j}]+Tensor[u1,{Void},{h}]*Tensor[\[CapitalGamma],{h,Void,Void},{Void,k,m}]*Tensor[\[CapitalGamma],{m,Void,Void},{Void,i,j}]+Tensor[u1,{Void},{m}]*PartialD[Tensor[\[CapitalGamma],{m,Void,Void},{Void,i,j}],k],PartialD[Tensor[u1,{i},{Void}],{j,k}]->CovariantD[Tensor[u1,{i},{Void}],{j,k}]+CovariantD[Tensor[u1,{i},{Void}],h]*Tensor[\[CapitalGamma],{h,Void,Void},{Void,j,k}]-CovariantD[Tensor[u1,{h},{Void}],j]*Tensor[\[CapitalGamma],{i,Void,Void},{Void,h,k}]-CovariantD[Tensor[u1,{m},{Void}],k]*Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,m}]+Tensor[u1,{h},{Void}]*Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,m}]*Tensor[\[CapitalGamma],{m,Void,Void},{Void,h,k}]-Tensor[u1,{m},{Void}]*PartialD[Tensor[\[CapitalGamma],{i,Void,Void},{Void,j,m}],k]}


(* ::Input::GrayLevel[0]:: *)
PartialDSimplify[expr_]:=Module[{pos,tab},expr/.PartialD[u1_][Tensor[v1_],w1_]:>(tab=Flatten[{(v1/.Times[uu_,vv__]->{uu,vv})}];(pos=(Position[Xor[FreeQ[#,w1],FreeQ[#,Tensor[#,{i_},{Void}]&@@w1]]&/@tab,True]));PartialD[u1][Tensor[Apply[Times,Delete[tab,pos]]],w1]If[pos=={},1,Product[tab[[pos[[i]]]],{i,Length[pos]}][[1]]])/.PartialD[u1_][Tensor[v1_,{},{}],w1_]:>(tab=Flatten[{(v1/.Times[uu_,vv__]->{uu,vv})}];(pos=(Position[Xor[FreeQ[#,w1],FreeQ[#,Tensor[#,{i_},{Void}]&@@w1]]&/@tab,True]));PartialD[u1][Tensor[Apply[Times,Delete[tab,pos]]],w1]If[pos=={},1,Product[tab[[pos[[i]]]],{i,Length[pos]}][[1]]])]


(* ::Input::GrayLevel[0]:: *)
TGrad[\[DoubleStruckE]_,flavor_@i_][f_]:=Module[{i$},CovariantD[f,flavor@i$]Tensor[\[DoubleStruckE],{flavor@i$},{Void}]/.i$->i]
TGrad[\[DoubleStruckE]_,i_][f_]:=If[Head@i==Symbol,Module[{i$},CovariantD[f,i$]Tensor[\[DoubleStruckE],{i$},{Void}]/.i$->i]]


(* ::Input::GrayLevel[0]:: *)
TDiv[\[DoubleStruckE]_,g_,flavor_@i_,metricsimplify_:True][vector_]:=Module[{i$},HoldOp[CovariantD][CovariantD[vector.Tensor[\[DoubleStruckE],{flavor@i$},{Void}]//EvaluateDotProducts[\[DoubleStruckE],g,metricsimplify],flavor@i$]/.i$->i]]
TDiv[\[DoubleStruckE]_,g_,i_,metricsimplify_:True][vector_]:=If[Head@i==Symbol,Module[{i$},HoldOp[CovariantD][CovariantD[vector.Tensor[\[DoubleStruckE],{i$},{Void}]//EvaluateDotProducts[\[DoubleStruckE],g,metricsimplify],i$]/.i$->i]]]
TDiv[\[DoubleStruckE]_,g_,flavor_@i_][vector_]:=TDiv[\[DoubleStruckE],g,flavor@i,True][vector]//ReleaseHold
TDiv[\[DoubleStruckE]_,g_,i_][vector_]:=TDiv[\[DoubleStruckE],g,i,True][vector]//ReleaseHold


(* ::Input::GrayLevel[0]:: *)
TCurl[\[DoubleStruckE]_,g_,flavor_/@{i_,j_,k_},e_,metricsimplify_:True][vector_]:=Module[{i\[Section],j\[Section],k\[Section]},CovariantD[vector.Tensor[\[DoubleStruckE],{Void},{flavor@j\[Section]}]//EvaluateDotProducts[\[DoubleStruckE],g,metricsimplify],flavor@i\[Section]]Tensor[\[DoubleStruckE],{Void},{flavor@k\[Section]}]Tensor[e,{flavor@i\[Section],flavor@j\[Section],flavor@k\[Section]},{Void,Void,Void}]/.{i\[Section]->i,j\[Section]->j,k\[Section]->k}]
TCurl[\[DoubleStruckE]_,g_,{i_,j_,k_},e_,metricsimplify_:True][vector_]:=If[Head@i ==Symbol&&Head@j==Symbol &&Head@k==Symbol,Module[{i\[Section],j\[Section],k\[Section]},CovariantD[vector.Tensor[\[DoubleStruckE],{Void},{j\[Section]}]//EvaluateDotProducts[\[DoubleStruckE],g,metricsimplify],i\[Section]]Tensor[\[DoubleStruckE],{Void},{k\[Section]}]Tensor[e,{i\[Section],j\[Section],k\[Section]},{Void,Void,Void}]/.{i\[Section]->i,j\[Section]->j,k\[Section]->k}]]


MetricSimplifyD[g_][expr_]:=Module[{CvD,CtvD,CvD2d,CtvD2d},(((((expr/.{CovariantD->CvD,GhostCovariantD2d->CovariantD}//MetricSimplify[g])/.{CovariantD->GhostCovariantD2d,CvD->CovariantD}
/.HoldForm[CovariantD[expr1__]]->CvD[expr1]//MetricSimplify[g])/.CvD[expr2__]->HoldForm[CovariantD[expr2]]/.HoldForm[ContravariantD[expr1__]]->CtvD[expr1]//MetricSimplify[g])/.CtvD[expr2__]->HoldForm[ContravariantD[expr2]]/.HoldForm[CovariantD2d[expr1__]]->CvD2d[expr1]//MetricSimplify[g])/.CvD2d[expr2__]->HoldForm[CovariantD2d[expr2]]/.HoldForm[ContravariantD2d[expr1__]]->CtvD2d[expr1]//MetricSimplify[g])/.CtvD2d[expr2__]->HoldForm[ContravariantD2d[expr2]]]


(* ::Input::GrayLevel[0]:: *)
TLaplacian[\[DoubleStruckE]_,g_,i_,j_,metricsimplify_:True][f_]:=If[i=!=j,TDiv[\[DoubleStruckE],g,i,metricsimplify][TGrad[\[DoubleStruckE],j][f]]]

TLaplacian[\[DoubleStruckE]_,g_,i_][f_]:=Module[{res},Module[{j,e},res=TLaplacian[\[DoubleStruckE],g,i,j][f];MetricSimplifyD[g][res]/.{i->j,j->i}]]

TLaplacian[\[DoubleStruckE]_,g_,flavor_@i_,flavor_@j_,metricsimplify_:True][f_]:=If[i=!=j,TDiv[\[DoubleStruckE],g,flavor@i,metricsimplify][TGrad[\[DoubleStruckE],flavor@j][f]]]

TLaplacian[\[DoubleStruckE]_,g_,flavor_@i_][f_]:=Module[{res},Module[{j,e},res=TLaplacian[\[DoubleStruckE],g,flavor@j,flavor@i][f];MetricSimplifyD[g][res]/.{i->j,j->i}]]


(* ::Input::GrayLevel[0]:: *)
LaplaceOp[\[CapitalDelta]_][expr_]:=expr//.{CovariantD[Tensor[t__], {i_, Void}, {Void, i_}]:>\[CapitalDelta][Tensor[t]],CovariantD[Tensor[t__], {Void, i_}, {i_, Void}]:>\[CapitalDelta][Tensor[t]]}//.{CovariantD[Tensor[t__], {j__,i_, Void}, {k__,Void, i_}]:>\[CapitalDelta][CovariantD[Tensor[t],{j},{k}]],CovariantD[Tensor[t__], {j__,Void, i_}, {k__,i_, Void}]:>\[CapitalDelta][CovariantD[Tensor[t],{j},{k}]]}//.{CovariantD[Tensor[t__], {i_, Void,j__}, {Void, i_,k__}]:>\[CapitalDelta][CovariantD[Tensor[t],{j},{k}]],CovariantD[Tensor[t__], {Void, i_,j__}, {i_, Void,k__}]:>CovariantD[\[CapitalDelta][Tensor[t]],{j},{k}]}//.{\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "t__", "]"}], ",", 
RowBox[{"{", 
RowBox[{"i_", ",", "j_", ",", "Void", ",", "Void"}], "}"}], ",", 
RowBox[{"{", 
RowBox[{"Void", ",", "Void", ",", "i_", ",", "j_"}], "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>\[CapitalDelta][\[CapitalDelta][Tensor[t]]],\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", "t__", "]"}], ",", 
RowBox[{"{", 
RowBox[{"Void", ",", "Void", ",", "i_", ",", "j_"}], "}"}], ",", 
RowBox[{"{", 
RowBox[{"i_", ",", "j_", ",", "Void", ",", "Void"}], "}"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>\[CapitalDelta][\[CapitalDelta][Tensor[t]]]}


(* ::Input::GrayLevel[0]:: *)
FunctionalD[g_,flv_][L_,f_]:=FunctionalD[g,flv,r$,s$][L,f]

FunctionalD[g_,flv_,r$_,s$_][L_,f_]:=Module[{cov,cov2d,pos,pos1,term,U},
(((CovariantD[L//CovNotation[cov]//Cov2dNotation[cov2d],f$]/.f$->f/.ruleDFunct[g])-CovariantD[(CovariantD[L//CovNotation[cov]//Cov2dNotation[cov2d],f$]/.f$:>(CovariantD[f,flv@r$]//CovNotation[cov]//Cov2dNotation[cov2d])/.ruleDFunct[g]),flv@r$]+CovariantD[(CovariantD[L//CovNotation[cov]//Cov2dNotation[cov2d],f$]/.f$:>(CovariantD[f,{flv@r$,flv@s$}]//CovNotation[cov]//Cov2dNotation[cov2d])/.ruleDFunct[g]),{flv@r$,flv@s$}])//MetricSimplify[g]//CovariantDSimplify[g])/.Tensor[t_,{u1__},{v1__}]:>If[(pos1=Position[{{u1},{v1}}//Transpose,cov@_])==={},Tensor[t,{u1},{v1}],{(pos=pos1//Transpose),term=If[Length[pos1]==Length[{u1}],Tensor[t],Tensor[t,U=Delete[{u1},List/@pos[[1]]],Delete[{v1},List/@pos[[1]]]]],Do[If[pos[[2,ii]]==2,term=CovariantD[term,{v1}[[ii+Length[U]]]],term=ContravariantD[term,{u1}[[ii+Length[U]]]]],{ii,Length[pos[[2]]]}],term/.cov->Identity}[[4]]]/.Tensor[t_,{u1__},{v1__}]:>If[(pos1=Position[{{u1},{v1}}//Transpose,cov2d@_])==={},Tensor[t,{u1},{v1}],{(pos=pos1//Transpose),term=If[Length[pos1]==Length[{u1}],Tensor[t],Tensor[t,U=Delete[{u1},List/@pos[[1]]],Delete[{v1},List/@pos[[1]]]]],Do[If[pos[[2,ii]]==2,term=CovariantD2d[term,{v1}[[ii+Length[U]]]],term=ContravariantD2d[term,{u1}[[ii+Length[U]]]]],{ii,Length[pos[[2]]]}],term/.cov2d->Identity}[[4]]]
]

ruleDFunct[g_]:=(\!\(\*
TagBox[
StyleBox[
RowBox[{"CovariantD", "[", 
RowBox[{
RowBox[{"Tensor", "[", 
RowBox[{"TN_", ",", 
RowBox[{"{", "i$__", "}"}], ",", 
RowBox[{"{", "j$__", "}"}]}], "]"}], ",", 
RowBox[{"List", "[", "Void", "]"}], ",", 
RowBox[{"List", "[", 
RowBox[{"Tensor", "[", 
RowBox[{"TD_", ",", 
RowBox[{"{", "k$__", "}"}], ",", 
RowBox[{"{", "l$__", "}"}]}], "]"}], "]"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\):>(If[TN===TD && Length[{i$}]==Length[{k$}],Module[{gNum,gDen,g$},({gNum=StandardDownIndices[g$][Tensor[TN,{i$},{j$}]]/.(Tensor[T_,{__},down__]/;T=!=g$):>Num[down],gDen=StandardDownIndices[g$][Tensor[TD,{k$},{l$}]]/.(Tensor[T_,{__},down__]/;T=!=g$):>Den[down]};gNum  (gDen/.Tensor[g$,{i__},{j__}]:>Tensor[g$,{j},{i}])/.Num[ij_]Den[kl_]:>Product[Tensor[g$,{kl[[ind]],Void},{Void,ij[[ind]]}],{ind,Length[{i$}]}]/.Tensor[g$,{Cov[m$_],Void},{Void,Cov[n$_]}]->Tensor[g$,{m$,Void},{Void,n$}])/.{g$->g}],0]))


(* ::Input::GrayLevel[0]:: *)
End[]


(* ::Input::GrayLevel[0]:: *)
EndPackage[]


(* ::Input::GrayLevel[0]:: *)
SetContravariantDisplay["SemicolonMode"];
SetContravariantD2display["AlternativesMode"];
 TContinuumMechanics3`TContinuumMechanics`Private`CovSym=";"; TContinuumMechanics3`TContinuumMechanics`Private`Cov2dSym="|\!\(\*
StyleBox[\" \",\nFontSize->2]\)"; TContinuumMechanics3`TContinuumMechanics`Private`DifSym=",";
SymbolSpaceDimension[\[DoubleStruckE]_]=NDim;
TContinuumMechanics3`TContinuumMechanics`Private`replacement[i_]:=i
